/**
 * Copyright 2009 Wisdom
 */
package com.wisdom.model.entities;

import java.util.Date;
import java.util.Set;

import javax.persistence.*;

/**
 * Entity represent of user post in system
 * 
 * @author alekseiko
 * @since 1.0
 */
@Entity
@Table(name = "WE_POSTS")
public class Post {

    private long id;
    private String theme;
    private String post;
    private Date postDate;
    private int votes;
    private int viewCount;
    private User owner;
    private Category category;
    private Set<Keyword> keywords;
    private Set<Comment> comments;

    /**
     * Default constructor.
     */
    public Post() {
    }

    /**
     * Constructor initialize all fields.
     * 
     * @param id
     *            - unique post id
     * @param theme
     *            - post theme
     * @param post
     *            - post text
     * @param postDate
     *            - posted date
     * @param votes
     *            - votes for this question, will be negative or positive.
     * @param viewCount
     *            - how mach user view this post
     * @param owner
     *            - user how posted this is post
     * @param category
     *            - post category
     * @param keywords
     *            - post keywords
     * @param comments - comments for this post
     */
    public Post(long id, String theme, String post, Date postDate, int votes,
	    int viewCount, User owner, Category category,
	    Set<Keyword> keywords, Set<Comment> comments) {
	this.id = id;
	this.theme = theme;
	this.post = post;
	this.postDate = postDate;
	this.votes = votes;
	this.viewCount = viewCount;
	this.owner = owner;
	this.keywords = keywords;
	this.category = category;
	this.comments = comments;
    }

    /**
     * Get unique post id in system.
     * 
     * @return the id
     * 
     */
    @Id
    @Column(name = "WE_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public long getId() {
	return id;
    }

    /**
     * Set unique post id.
     * 
     * @param id
     *            - unique post id in system
     * 
     */
    public void setId(long id) {
	this.id = id;
    }

    /**
     * Get post theme.
     * 
     * @return the theme
     */
    @Column(name = "WE_THEME", nullable = false, updatable = true)
    public String getTheme() {
	return theme;
    }

    /**
     * Set post theme.
     * 
     * @param theme
     *            - post theme
     */
    public void setTheme(String theme) {
	this.theme = theme;
    }

    /**
     * Get post text.
     * 
     * @return the post
     */
    @Column(name = "WE_POST", nullable = false, updatable = true)
    public String getPost() {
	return post;
    }

    /**
     * Set post text.
     * 
     * @param post
     *            - post text
     */
    public void setPost(String post) {
	this.post = post;
    }

    /**
     * Get post date.
     * 
     * @return the postDate
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "WE_POST_DATE", nullable = false, updatable = true)
    public Date getPostDate() {
	return postDate;
    }

    /**
     * Set post date.
     * 
     * @param postDate
     *            - posted date
     */
    public void setPostDate(Date postDate) {
	this.postDate = postDate;
    }

    /**
     * Get post votes.
     * 
     * @return the votes
     */
    @Column(name = "WE_VOTES", nullable = false, updatable = true)
    public int getVotes() {
	return votes;
    }

    /**
     * Set post voites.
     * 
     * @param votes
     *            - votes for this post, will be negative or positive.
     */
    public void setVotes(int votes) {
	this.votes = votes;
    }

    /**
     * Get view count.
     * 
     * @return the viewCount
     */
    @Column(name = "WE_VIEW_COUNT", nullable = false, updatable = true)
    public int getViewCount() {
	return viewCount;
    }

    /**
     * Set view count.
     * 
     * @param viewCount
     *            - how mach user view this question
     */
    public void setViewCount(int viewCount) {
	this.viewCount = viewCount;
    }

    /**
     * Get post owner
     * 
     * @return the owner
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "WE_OWNER_ID")
    public User getOwner() {
	return owner;
    }

    /**
     * Set post owner
     * 
     * @param owner
     *            - user how posted this is post
     */
    public void setOwner(User owner) {
	this.owner = owner;
    }

    /**
     * Get post category
     * 
     * @return the category
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "WE_CATEGORY_ID")
    public Category getCategory() {
	return category;
    }

    /**
     * Set post category.
     * 
     * @param category
     *            - post category
     */
    public void setCategory(Category category) {
	this.category = category;
    }

    /**
     * Get post keywords
     * 
     * @return the keywords
     */
    @OneToMany(mappedBy = "post")
    public Set<Keyword> getKeywords() {
	return keywords;
    }

    /**
     * Set post keywords
     * 
     * @param keywords
     *            - post keywords
     */
    public void setKeywords(Set<Keyword> keywords) {
	this.keywords = keywords;
    }

    /**
     * Get post comments
     * 
     * @return the comments
     */
    @OneToMany(mappedBy="post")
    public Set<Comment> getComments() {
        return comments;
    }

    /**
     * Set post comments
     * 
     * @param comments - comments for this post
     */
    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + (int) (id ^ (id >>> 32));
	return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	Post other = (Post) obj;
	if (id != other.id)
	    return false;
	return true;
    }

}
